# Copyright 2017 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

def expect_network_create(id, expected_body, data = {})
  default_props = {
    zone: 'test name#0 data',
    project: 'test project#0 data'
  }

  merged_uri = uri_data(id).merge(default_props).merge(data)
  body = { kind: 'compute#operation',
           status: 'DONE', targetLink: self_link(merged_uri) }.to_json
  # Remove refs that are also part of the body
  expected_body = Hash[expected_body.map do |k, v|
    [k.is_a?(Symbol) ? k.id2name : k, v]
  end]

  request = double('request')
  allow(request).to receive(:send).and_return(http_success(body))

  debug_network "!! POST #{collection(merged_uri)}"
  expect(Google::Compute::Network::Post).to receive(:new)
    .with(gcompute_disk_snapshot_uri(merged_uri),
          instance_of(Google::FakeAuthorization),
          'application/json', expected_body.to_json) do |args|
    debug_network ">> POST #{args} = body(#{body})"
    request
  end
end

def gcompute_disk_snapshot_uri(data)
  URI.parse(
    format(
      '%s/%s',
      Puppet::Type.type(:gcompute_disk).provider(:google).self_link(
        name: data[:name], zone: data[:zone], project: data[:project]
      ), 'createSnapshot'
    )
  )
end
